/*
 * @lc app=leetcode id=80 lang=cpp
 *
 * [80] Remove Duplicates from Sorted Array II
 */
class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        int len = nums.size();
        if (len <= 1) {
            return len;
        }

        int i   = 1;
        int idx = 0;
        int length = 1;
        bool isDup = false;

        while (i < len) {
            if (nums[i] == nums[idx]) {
                if (!isDup) {
                    idx++;
                    nums[idx] = nums[i];
                    length++;
                } 
                isDup = true;
            } else {
                idx++;
                nums[idx] = nums[i];
                length++;
                isDup = false;
            }
            i++;
        }
        return length;
    }
};

